﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class BaseManager
    {
        /// <summary>
        /// 获取登录用户配置的审核权限
        /// </summary>
        /// <param name="path">传入Xml文件下的路径 例如：AdverseEvent\\ReviewProcess.xml</param>
        /// <returns></returns>
        public static Job GetReviewProcess(string path)
        {
            Loginuser loginUser = LoginManager.GetLoginUser();
            ReviewProcess reviewProcess = new ReviewProcess();
            reviewProcess = XmlHelper.ReadXML<ReviewProcess>(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Xml\\" + path, reviewProcess);
            Job job = new Job();
            foreach (var item in reviewProcess.Jobs)
            {
                if (item.Title == loginUser.JobTitle)
                {
                    job = item;
                    break;
                }
            }
            return job;
        }

        /// <summary>
        /// 根据配置文件校验状态
        /// </summary>
        /// <param name="path">传入Xml文件下的路径 例如：AdverseEvent\\ReviewProcess.xml</param>
        /// <param name="newStatus"></param>
        /// <param name="oldStatus"></param>
        /// <returns></returns>
        public static bool CheckReviewProcess(string path, int newStatus, int oldStatus)
        {
            bool flag = false;
            Job job = GetReviewProcess(path);
            if (!string.IsNullOrWhiteSpace(job.Title))
            {
                foreach (var item in job.Status)
                {
                    if (item.Value == oldStatus)
                    {
                        foreach (var item1 in item.ViewBtns)
                        {
                            if (flag) break;
                            foreach (var item2 in item1.DetailsViewBtns)
                            {
                                if (item2.Parameter == newStatus.ToString())
                                {
                                    flag = true;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            return flag;
        }
    }
}
